//////////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2014-present, Egret Technology.
//  All rights reserved.
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the Egret nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
//  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//  IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
//  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
//  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
//  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////////////////
declare function In()
class LoadingUI extends egret.Sprite {

    public constructor() {
        super();
        this.createView();
    }

    private textField:egret.TextField;
    private bmp:egret.Bitmap;
    private sp:egret.Sprite;
    private sp2:egret.Sprite;
    private bmp_music:egret.Bitmap;
    private sc_bg:egret.SoundChannel;
    private sd_bg:egret.Sound;
    private sc_pos:number=0;
    private bg:egret.Shape;
     private createView():void {
       
       this.bg=new egret.Shape();
	   this.bg.graphics.beginFill(0x000000,1);
	   this.bg.graphics.drawRect(0,0,740,1136);
	//    this.addChild(this.bg);
       this.bg.alpha=0;
       egret.Tween.get(this.bg).to({alpha:1},1500,egret.Ease.cubicInOut);

       console.log(RES.getRes('bg_mp3'))
       this.sd_bg=RES.getRes('bg_mp3');

       this.bmp=new egret.Bitmap();
       this.bmp.texture=RES.getRes('LOGO_png');
       this.bmp.name='l';
       this.bmp.anchorOffsetX=this.bmp.width/2;
       this.bmp.x=740/2;
       this.bmp.y=1136/2-100;
    //    this.addChild(this.bmp);

       this.bmp_music=new egret.Bitmap();
       this.bmp_music.texture=RES.getRes('music_png');
       this.bmp_music.name='l';
       this.bmp_music.anchorOffsetX=this.bmp_music.width/2;
       this.bmp_music.anchorOffsetY=this.bmp_music.height/2;
       this.bmp_music.x=688;
       this.bmp_music.y=60;
       this.bmp_music.touchEnabled=true;
       this.bmp_music.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.touchHandler,this);
       
        this.textField = new egret.TextField();
        // this.addChild(this.textField);
        this.textField.x = 370;
        this.textField.y = 942;
        this.textField.width = 480;
        this.textField.height = 100;
        this.textField.textColor=0xEF7C1B;
        this.textField.anchorOffsetX=this.textField.width/2;
        this.textField.anchorOffsetY=this.textField.height/2;
        this.textField.size=90;
        this.textField.textAlign = "center";

        this.sp=new egret.Sprite();
        this.sp.graphics.beginFill(0xFFFFFF,1);
        this.sp.graphics.drawRect(0,0,350,4);
        this.sp.anchorOffsetY=this.sp.height/2;
        this.sp.x=(740-this.sp.width)/2;
        this.sp.y=1136/2+0;
        this.sp.alpha=1;
        // egret.Tween.get(this.sp).to({alpha:1},700,egret.Ease.cubicIn);

        this.sp2=new egret.Sprite();
        this.sp2.graphics.beginFill(0xff6600,1);
        this.sp2.graphics.drawRect(0,0,350,4);
        this.sp2.anchorOffsetX=0;
        this.sp2.anchorOffsetY=this.sp2.height/2;
        this.sp2.x=(740-this.sp2.width)/2;
        this.sp2.y=1136/2+0;
        this.sp2.scaleX=0;
        this.sp2.alpha=1;
        // egret.Tween.get(this.sp2).to({alpha:1},700,egret.Ease.cubicIn);

        this.addChild(this.sp);
        this.addChild(this.sp2);

    }
    private touchHandler(e:egret.TouchEvent):void
    {
        
    }

    
    public setProgress(current:number, total:number):void {
        console.log(current,total);
        // this.textField.text = `Loading...${current}/${total}`;
        // this.textField.text=String(Math.floor(100*current/total));
        this.sp2.scaleX=current/total;
        var myThis=this;
        if(current/total==1)
        {            
            // egret.Tween.get(this.bmp).pause();
            // egret.Tween.pauseTweens(this.bmp);
            // egret.Tween.get(this.bmp).wait(1500).to({alpha:0,y:-700},1000,egret.Ease.backIn);
            // egret.Tween.get(this.sp2).wait(1500).to({alpha:0},1000);
            // egret.Tween.removeTweens(this.bmp);
            // egret.Tween.removeTweens(this.sp);
            // egret.Tween.removeTweens(this.sp2);

            // egret.Tween.get(this.bmp).to({alpha:0},2500,egret.Ease.cubicIn);
            egret.Tween.get(this.sp).to({alpha:0},1500,egret.Ease.cubicIn);
            egret.Tween.get(this.sp2).to({alpha:0},1500,egret.Ease.cubicIn);

            setTimeout(function():void
            {               
                myThis.parent.removeChild(myThis);
                document.getElementById('canvas-three').style.zIndex='3';
                document.getElementById('img').style.zIndex='2';
                document.getElementById('canvas-egret').style.zIndex='1';
                document.getElementById('img_info').style.zIndex='55';
                document.getElementById('img_info').style.width=460*(window.innerWidth/740)+"px";
                document.getElementById('img_info').style.height=56*(window.innerWidth/740)+"px";
                document.getElementById('img_info').style.left=(window.innerWidth-parseInt(document.getElementById('img_info').style.width))/2+"px";
                document.getElementById('img_info').style.top =(window.innerHeight-parseInt(document.getElementById('img_info').style.height)-30)+"px";
                In();
            },1500);
            
        }
    }
    public playMusic():void
    {
          this.sc_bg=this.sd_bg.play(this.sc_pos,0);
    }
     public stopMusic():void
    {
        if(this.sc_bg)
			{
                console.log('stop!!');
				this.sc_pos=this.sc_bg.position;
				this.sc_bg.stop();
				this.sc_bg=null;
			}
    }
}
